let url = 'https://www.touzhnet.com/trim/batch/json'
// let url = 'http://192.168.0.109:8082/trim/batch/json'
// let url = 'http://192.168.0.109:8082/trim/batch/json'

// const qiniuUploader = require("/utils/qiniuUploader");
const qiniu = require("/utils/qiniuUploader");
wx.$qiniu = (files, success) => {
  let arr = new Array(files.length)
  let num = 0
  wx.showLoading()
  for (let x in files) {
    qiniu.upload(files[x], (res) => {
      arr[x] = res.imageURL
      num++
      if (num == files.length) {
        arr.map(v => {
          return v.imageURL
        })
        wx.hideLoading()
        success(arr)
      }
    }, res => {
      wx.showToast({
        title: '上传失败',
        icon: 'none'
      })
    }, {
      region: 'SCN',
      domain: 'http://file.touzhnet.com/',
      key: `${new Date().getTime()}.jpg`,
      uptoken: wx.getStorageSync('upToken'),
    })
  }
}


wx.$https = function (
  action = '',
  args = {},
  method = 'post',
  // head = {
  //   'Content-Type': 'application/json',
  //   'X-Nideshop-Token': wx.getStorageSync('token')
  // },
) {
  var data = {
    token: wx.getStorageSync("token"),
    command: {
      action: action,
      args: args
    }
  }
  wx.showLoading({
    mask: true,
    title: ''
  })
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      // head: head,
      success: function (res) {
        wx.hideLoading()
        if (res.data.return_code != 0) {
          wx.showToast({
            icon: 'none',
            title: res.data.return_msg,
          })
        } else {
          resolve(res.data.return_data)
        }
      },
      fail: function (res) {
        reject(res)
      }
    })
  })
}

wx.$nav = function (url, para) {
  let token = wx.getStorageSync('token')
  if (false) {
    wx.reLaunch({
      url: '/pages/login/login',
    })
  } else {
    let paras = ''
    for (let x in para) {
      paras += `${paras==''?'?':'&'}${x}=${para[x]}`
    }
    wx.navigateTo({
      url: url + paras,
    })
  }
}

wx.$redirectTo = function (url, para) {
  let paras = ''
  for (let x in para) {
    paras += `${paras==''?'?':'&'}${x}=${para[x]}`
  }
  wx.redirectTo({
    url: url + paras,
  })
}


wx.$reLaunch = function (url, para) {
  let paras = ''
  for (let x in para) {
    paras += `${paras==''?'?':'&'}${x}=${para[x]}`
  }
  wx.reLaunch({
    url: url + paras,
  })
}

wx.$getPrev = function (that, obj) {
  let pages = getCurrentPages();
  let prevPage = pages[pages.length - 2];
  for (let x in obj) {
    that.setData({
      [`${x}`]: prevPage.data[obj[x]]
    })
  }
}

wx.$prev = function (obj) {
  let pages = getCurrentPages();
  let prevPage = pages[pages.length - 2];
  for (let x in obj) {
    prevPage.setData({
      [`${x}`]: obj[x]
    })
  }
}

wx.$unifiedorder = function (id, sFun, fFun, isOrder = true) {
  wx.showLoading({
    title: '正在支付...'
  })
  wx.$https('app.payment', {
    [`${isOrder?'ordersId':'actuaryId'}`]: id,
  }).then(ress => {
    wx.hideLoading();
    var order = ress;
    //吊起微信支付
    wx.requestPayment({
      timeStamp: order.timeStamp + "",
      nonceStr: order.nonceStr,
      package: order.package,
      signType: 'MD5',
      paySign: order.paySign,
      success: function (res) {
        sFun && sFun()
      },
      fail: function (res) {
        // 用户取消支付
        if ("requestPayment:fail cancel" == res.errMsg) {
          fFun && fFun()
        }
      }
    })
  })

}

wx.$coupon = function (res) {
  let coupon = new Array
  for (let x in res) {
    for (let y in res[x]) {
      coupon.push(res[x][y])
    }
  }
  return coupon
}


wx.$isLogin = function (callback = () => {}, url = '', params = {}) {
  let token = wx.getStorageSync('token')
  if (!token) {
    wx.$reLaunch('/pages/login/login', {
      url: JSON.stringify(url),
      params: JSON.stringify(params)
    })
  } else {
    callback()
  }
}